package org.fuxia.im.core.socket;

import java.util.Scanner;

import org.fuxia.im.core.ECMessageType;
import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.iconst.Constant;
import org.fuxia.im.core.socket.impl.ECNioClient;

public class ECNioClientDemo
{
	static ECNioClient	client	= null;

	public static void main(String[] args) throws Exception
	{
		client = new ECNioClient("localhost", 8989);
		FilePacketInfoListener fl = new FilePacketInfoListener();
		client.registPacketListener(fl, ECMessageType.FILE_TRANSFER_INFO.name());
		client.registPacketListener(fl, ECMessageType.FILE_TRANSFER_ACK.name());
		new Thread(client).start();
		boolean canRun = true;
		Scanner s = new Scanner(System.in);
		StringBuilder sb = new StringBuilder(
				"=============客户端操作==============\n");
		sb.append("<1> 测试登陆\n");
		sb.append("<2> 测试发送消息\n");
		sb.append("<3> 测试发送文件\n");
		sb.append("<4> 测试发送文件\n");
		sb.append("<0> 退出系统\n");
		sb.append("=================================");
		while (canRun)
		{
			System.out.println(sb);
			System.out.print("请输入操作编号:");
			String v = s.next();
			int opNum = -1;
			if (v.matches("\\d{1}"))
			{
				opNum = Integer.parseInt(v);
			}
			else
			{
				continue;
			}
			switch (opNum)
			{
			case 1:
				String userCode = null;
				String password = null;
				System.out.print("请输入账户名：");
				userCode = s.next();
				System.out.print("请输入密码：");
				password = s.next();
				testLogin(userCode, password);
				break;
			case 2:
				System.out.println("请输入需要发送的消息");
				String msString = s.next();
				msString = new String(msString.getBytes("GBK"));
				System.out.println(msString);
				System.out.println("请输入发送消息的用户");
				String from = s.next();
				System.out.println("请输入发送到的用户");
				String to = s.next();
				testSendMessage(msString, from, to);
				break;
			case 4:
				System.out.println("请输入发送文件名,发送用户,接收用户");
				String fileName = s.next();
				fileName = new String(fileName.getBytes("GBK"));
				testFileTransfer(fileName, s.next(), s.next());
				break;
			case 0:
				System.out.println("请输入需要注销的用户");
				String user = s.next();
				testLogout(user);
				canRun = false;
				client.shutdown();
				break;
			default:
				break;
			}
		}

	}

	public static void testLogin(String user , String pass) throws Exception
	{
		ECPacket packet = new ECPacket();
		packet.setMessageType(ECMessageType.LOGIN.name());
		packet.addParam(Constant.USERCODE.name(), user);
		packet.addParam(Constant.PASSWORD.name(), pass);
		client.sendMessage(packet);
	}

	public static void testLogout(String user) throws Exception
	{
		ECPacket packet = new ECPacket();
		packet.setMessageType(ECMessageType.LOGOUT.name());
		packet.addParam(Constant.USERCODE.name(), user);
		client.sendMessage(packet);
	}

	public static void testSendMessage(String message , String from , String to) throws Exception
	{
		String[] toC = to.contains(",") ? to.split(",") : new String[] { to };
		client.sendTextMessage(message, from, toC);
	}

	public static void testFileTransfer(String fileName , String from , String to) throws Exception
	{

		ECPacket p = new ECPacket();
		p.setMessageType(ECMessageType.FILE_TRANSFER_INFO.name());
		p.addParam("fileName", fileName);
		p.addParam("fileSize", String.valueOf(1024 * 1024));
		p.setFrom(from);
		p.setTo(to);
		client.sendMessage(p);
	}
}
